package createnewobject;

import java.io.*;
import java.lang.reflect.Constructor;

public class CreateNewObjectFourMethodDemo {

    public static void main(String[] args) throws Exception{
        //1.使用new关键字
        Person p1 = new Person("张三","男","1999-10-10");
        System.out.println(p1);

        //2.使用clone
        Person p2 = (Person) p1.clone();
        System.out.println(p2);

        //3.使用对象反序列化
        File destFile = new File("reflection/src/createnewobject/save.data");
        ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(destFile));
        ObjectInputStream in = new ObjectInputStream( new FileInputStream(destFile));

        out.writeObject(p1);
        Person p3 = (Person) in.readObject();
        System.out.println(p3);
        out.close();
        in.close();

        //4.使用反射创建对象。
        //获取类对象
        Class clazz = Class.forName("createnewobject.Person");
        //通过反射获取带三个参数的构造方法对象。
        Constructor constructor = clazz.getDeclaredConstructor(String.class, String.class, String.class);
        Person p4 = (Person) constructor.newInstance("张三", "男", "1999-10-10");
        System.out.println(p4);

        System.out.println(p1 == p2);
        System.out.println(p2 == p3);
        System.out.println(p3 == p4);
    }
}
